package com.fresh.liangyi.controller.client;

import com.fresh.liangyi.model.domain.Goods;
import com.fresh.liangyi.model.domain.Menu;
import com.fresh.liangyi.model.domain.Order;
import com.fresh.liangyi.model.response_data.ResponseData;
import com.fresh.liangyi.service.GoodsService;
import com.fresh.liangyi.service.MenuService;
import com.fresh.liangyi.service.OrderService;
import com.fresh.liangyi.service.UserService;
import com.fresh.liangyi.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private MenuService menuService;

    @GetMapping(value = {"","/index"})
    public String index(@RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "count",defaultValue = "10") Integer count,
                        Model model){
        List<Order> orderList=orderService.showOrder();
        model.addAttribute("orderList",orderList);
        return "back/order_list";
    }
    //订单列表
    @GetMapping("/order")
    public String myOrder(@RequestParam(value = "page",defaultValue = "1") Integer page,
                          @RequestParam(value = "count",defaultValue = "10") Integer count,
                          Model model){
        List<Order> orderList=orderService.showOrder();
        model.addAttribute("orderList",orderList);
        return "back/order_list";
    }

    //发货
    @PostMapping("/sendOrder")
    @ResponseBody
    public String sendOrder(@RequestParam Integer id){
        Order order= orderService.selectOrderById(id);
        order.setStatus(2);
        if(orderService.updateOrder(order)!=0){
            return "success";
        }else{
            return "fail";
        }
    }

    @GetMapping("/toAddGoods")
    public String toAddGoods(){
        return "back/goods_edit";
    }
    //发布商品
    @PostMapping("/goods/publish")
    @ResponseBody
    public ResponseData publish(Goods goods){
        try{
            goodsService.addGoods(goods);
            log.info("商品发布成功"+goods);
            return ResponseData.ok();
        }catch (Exception e){
            log.warn("商品发布失败");
            return ResponseData.fail();
        }
    }

    @GetMapping("/toAddMenu")
    public String toAddMenu(){
        return "back/menu_edit";
    }
    //发布商品
    @PostMapping("/menu/publish")
    @ResponseBody
    public ResponseData publish(Menu menu){
        menu.setCreateDate(new Date());
        try{
            menuService.addMenu(menu);
            log.info("菜谱发布成功"+menu);
            return ResponseData.ok();
        }catch (Exception e){
            log.warn("菜谱发布失败"+menu);
            return ResponseData.fail();
        }
    }
}
